local app = require "app"
local game = require "club.threecard.hbsl"

local M = game.ui.element()

local function update(self, store, state)
    self.slider.value = state.selected_bet_index
    self.amount_txt.text = app.i18n.number(state.amount, "chip3")
end

M.store_watchers = {
    bet_amounts = {
        ["初始化下注档位"] = function(self, store, state, args)
            self.slider.minValue = 1
            self.slider.maxValue = #state.config
            update(self, store, state)
        end,

        ["改变下注"] = function(self, store, state, args, init)
            if init then
                return
            end
            update(self, store, state)
        end
    }
}

function M:ctor()
    self.res = "club.threecard.hbsl/ui/ingame.b:bet_operator"
end

function M:on_injected(bind)
    bind("button.click", self.btn_add, self.add_ivk)
    bind("button.click", self.btn_sub, self.sub_ivk)
    bind("slider.value", self.slider, self.slider_ivk)

    game.utility.button_style.highlight(self.btn_add)
    game.utility.button_style.highlight(self.btn_sub)
end

function M:add_ivk()
    game.store.bet_amounts.up()
    game.audio.post("hbsl:click")
end

function M:sub_ivk()
    game.store.bet_amounts.down()
    game.audio.post("hbsl:click")
end

function M:slider_ivk()
    local index = math.floor(self.slider.value)
    game.store.bet_amounts.set_index(index)
end


return M